/*
 *  Copyright 2003 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
/* "@(#) DDK 1.10.00.23 07-02-03 (ddk-b12)" */
#include <std.h>
#include <tsk.h>  
#include <sem.h>   
#include <gio.h>

#include <csl_dat.h>
#include <csl_cache.h>


#include <fvid.h>
#include <edc.h>
#include <vport.h>
#include <vportcap.h>
#include <vportdis.h>
#include <saa7105.h>
#include <tvp51xx.h>    

#include <evmdm642.h>
#define _LOOPBACK
//#define _PIP

#include "colorbarRGB.h"
#include "evmdm642_vcapparams.h"
#include "evmdm642_vdisparams.h"   
/* heap IDs defined in the BIOS configuration file */
extern Int EXTERNALHEAP;
#ifdef _LOOPBACK
static const  short coeffs[5] = {0x2543, 0x3313, -0x0C8A, -0x1A04, 0x408D};
#endif

/*
 * ======== main ========
 */
main()
{
    /******************************************************/
    /* open CSL DAT module for fast copy                  */
    /******************************************************/
    CSL_init();      
    
  //  EVMDM642_init();
                                           
    CACHE_clean(CACHE_L2ALL, 0, 0);
    CACHE_setL2Mode(CACHE_256KCACHE);       
    CACHE_enableCaching(CACHE_EMIFA_CE00);
    CACHE_enableCaching(CACHE_EMIFA_CE01);
    DAT_open(DAT_CHAANY, DAT_PRI_LOW, DAT_OPEN_2D);
}

/*
 * ======== tskVideoLoopback ========
 * video loopback function.
 */
void tskVideoLoopback()
{
    Int status;
    Int frames = 0;  
    Int numLinesCap = EVMDM642_vCapParamsChan.fldYStop1 -
       EVMDM642_vCapParamsChan.fldYStrt1+1;

    FVID_Handle capChan;
    FVID_Frame *capFrameBuf;
            
    /******************************************************/
    /* allocate both capture and display frame buffers    */
    /* in external heap memory                            */
    /******************************************************/
    EVMDM642_vCapParamsChan.segId = EXTERNALHEAP;
    EVMDM642_vCapParamsTVP51XX.hI2C = EVMDM642_I2C_hI2C;

    /******************************************************/
    /* initialization of capture driver                   */
    /******************************************************/
    capChan = FVID_create("/VP0CAPTURE/A/0", 
            IOM_INPUT, &status, (Ptr)&EVMDM642_vCapParamsChan, NULL);
         
    
    FVID_control(capChan, VPORT_CMD_EDC_BASE + EDC_CONFIG, 
        (Ptr)&EVMDM642_vCapParamsTVP51XX);

    EVMDM642_rset(0x10, 0x20); /* unlock DLL */
    while(!(EVMDM642_rget(0x13) & 0x40)); /* wait until DLL is locked */


    /******************************************************/
    /* start capture & display operation                  */    
    /******************************************************/
    FVID_control(capChan, VPORT_CMD_START, NULL);
      
    /********************************************************/
    /* request a frame buffer from display & capture driver */
    /********************************************************/
    FVID_alloc(capChan, &capFrameBuf);

    frames ++;


    while(1){/* loop forever */
        CACHE_clean(CACHE_L2ALL, 0, 0);    
        frames ++;              
   }
}
